package objects.fields;

import java.io.IOException;
import objects.AddressedObject;
import static objects.AddressedObject.objects;
import objects.DataObject;
import objects.slots.ArraySlot;
import objects.visualization.Block;
import objects.visualization.ListBlock;

public class ObjectArray extends AddressedObject {
	public DataObject[] value;

	public ObjectArray ( DataObject fromValue[] ) {
		this.value = fromValue;
		this.gruClass = arrayClass;
	}

	public ObjectArray () {
		this.value = new DataObject[ 0 ];
		this.gruClass = arrayClass;
	}

	public static ObjectArray create() {
		return create( new DataObject[ 0 ] );
	}

	public static ObjectArray create( DataObject[] value ) {
		ObjectArray array = new ObjectArray( value );
		objects.add( array );
		return array;
	}

	@Override
	public void io() throws IOException {
		if( read ) {
			value = arrayIO( value, file.readInt() );
		} else {
			file.writeInt( value.length );
			value = arrayIO( value, value.length );
		}
	}

	@Override
	public DataObject newObject() {
		return new ObjectArray( new DataObject[ 0 ] );
	}
	
	@Override
	public ObjectArray toArray() {
		return this;
	}

	Block toBlocks( int indent ) {
		ListBlock block = new ListBlock( true, true );
		block.add( new Block( new ArraySlot( this, 0, ArraySlot.Position.BEFORE ) ) );
		for( int n = 0; n < value.length; n++ ) {
			block.add( value[ n ].toModuleBlock( indent ) );
			block.add( new Block( new ArraySlot( this, n, ArraySlot.Position.AFTER ) ) );
		}
		return block;
	}
}
